package com.lzjt.controller;

import com.github.pagehelper.PageInfo;
import com.lzjt.model.DrugModel;
import com.lzjt.param.DrugParam;
import com.lzjt.service.DrugService;
import com.lzjt.util.Msg;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.RolesAllowed;
import java.util.Date;

@Api(tags="药品控制器类")
@RestController
@RequestMapping("/api/drugs")
@CrossOrigin


public class DrugController {
    @Autowired
    private DrugService drugService;
    @GetMapping("/{pn}/{size}")
    public Msg getDrugWithPage(@PathVariable("pn")int pn, @PathVariable("size") int size, @RequestParam(required=false)String name){
        PageInfo<DrugModel> info=drugService.getDrugWithPage(pn,size,name);
        if(info!=null){
            return Msg.success().data("drugPageInfo",info);
        }
        return Msg.fail();
    }
    @RolesAllowed({"1","2"})
    @PostMapping(value="")
    public Msg saveDrug(@RequestBody DrugParam drugParam){
        drugParam.setCreatetime(new Date());
        drugParam.setUpdatetime(new Date());
        Msg msg=drugService.saveDrug(drugParam);
        return msg;
    }

    @RolesAllowed({"1"})
    @PutMapping(value="/{id}")
    public Msg updateDrug(@PathVariable("id") Long id,@RequestBody DrugParam drugParam){
        return drugService.updateDrug(id,drugParam);
    }

    @RolesAllowed({"1"})
    @DeleteMapping(value="/{drugId}")
    public Msg deleteDrug(@PathVariable("drugId") Long drugId){
        return drugService.deleteDrug(drugId);
    }
}
